package com.mybatis_puls.demo.controller;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatis_puls.demo.entity.OrderS;
import com.mybatis_puls.demo.entity.R;
import com.mybatis_puls.demo.entity.User;
import com.mybatis_puls.demo.service.AddressService;
import com.mybatis_puls.demo.service.FileService;
import com.mybatis_puls.demo.service.OrderSService;
import com.mybatis_puls.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.jnlp.FileSaveService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小🌤
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/orderS")
public class OrderSController {
    @Autowired
    private OrderSService orderService;
   @Autowired
   private UserService userService;
   @Autowired
   private AddressService addressService;
   @Autowired
   private FileService fileService;

    /**
     * 保存订单
     * @param orders
     * @return
     */
    @RequestMapping("/save")
    public Object save(OrderS orders){
        List<User> users = userService.listByIds(Arrays.asList(orders.getUserId()));
     if(users.get(0).getShowFee()>=orders.getMoney()){
         users.get(0).setShowFee(users.get(0).getShowFee()-orders.getMoney());
          userService.updateById(users.get(0));
         orders.setTime(new Date());
         orders.setUuid(IdUtil.simpleUUID());
         orderService.save(orders);
         return orders;
     }
     return null;
    }

    /**
     * 查询
     * @param orderS
     * @return
     */
    @RequestMapping("/findBy")
    public  Object findBy (OrderS orderS,Integer page){
        page=page==null?1:page;
        //分页 第一个是分页的页数  定二个是每一页的大小
        Page<OrderS> page1 = new Page<>(page,5);

        QueryWrapper<OrderS> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("TIME ");//按时间排序最新的在前面
         if(orderS.getSchoolId()!=null){
             wrapper.eq("school_id",orderS.getSchoolId());
         }else if(orderS.getUserId()!=null){
              wrapper.eq("user_id",orderS.getUserId());
         }else if(orderS.getHelpId()!=null){
              wrapper.eq("help_id",orderS.getHelpId());
         }else if(orderS.getUuid()!=null){
             wrapper.eq("uuid",orderS.getUuid());
         }
         //条件分页查询
        Page<OrderS> page2 = orderService.page(page1, wrapper);
        if(page2.getRecords().size()>0){
             for (OrderS s : page2.getRecords()) {
                 s.setUser(userService.getById(s.getUserId()));
                 s.setAddress(addressService.getById(s.getAddressId()));
                 if(s.getHelpId()!=null&&s.getHelpId()!=null&&s.getHelpUser()!=null) {
                     s.setHelpUser(userService.listByIds(Arrays.asList(s.getHelpId())).get(0));
                 }
             }
         }

        return page2.getRecords();
    }

    @RequestMapping("/update")
     public Object update(OrderS orderS){
         orderService.updateById(orderS);
    return  orderS;
    }

    /**
     * 逻辑删除订单
     * @param uuid
     * @return
     */
     @RequestMapping("/delete/{uuid}")
    public Object delete(@PathVariable("uuid") String uuid){
         boolean b = orderService.removeById(uuid);
         return b;
     }

    /**
     * 修改订单状态
     * @param uuid
     * @param status
     * @return
     */
     @RequestMapping("/updateOver/{uuid}")
  public  Object updateOver(@PathVariable("uuid")String uuid,Integer status){
          OrderS order=new OrderS();
          order.setUuid(uuid);
          order.setStatus(status);
         boolean b = orderService.updateById(order);
    return b;
     }

    /**
     * 订单支付   订单完成
     * @param orderS
     * @return
     */
     @RequestMapping("/updateStatus")
     public  Object ordersUpdate(OrderS orderS){
         boolean b=false;
         try {
             orderService.updateById(orderS);
             List<OrderS> orderS1 = orderService.listByIds(Collections.singleton(orderS.getUuid()));
             if(orderS1.size()>0&&orderS1.get(0).getStatus()==3){
                 OrderS orderS2 = orderS1.get(0);
                 List<User> users = userService.listByIds(Collections.singleton(orderS2.getHelpId()));
                 User user = users.get(0);
                 user.setShowFee(user.getShowFee()+orderS2.getMoney());
                  b = userService.updateById(user);
             }
         } catch (Exception e) {
             b=false;
         }
         return b;
     }

    /**
     * 订单详情查询
     * @param uuid
     * @return
     */
     @RequestMapping("/findByid/{uuid}")
    public Object findById(@PathVariable("uuid")String uuid){
         List<OrderS> orderS = orderService.listByIds(Collections.singleton(uuid));
         if(orderS.size()>0){
             OrderS s = orderS.get(0);
              s.setUser(userService.getById(s.getUserId()));
              s.setAddress(addressService.getById(s.getAddressId()));
              s.setHelpUser(userService.getById(s.getHelpId()));
              if(s.getDetailId()!=null){
                   s.setFileS(fileService.getById(s.getDetailId()));
              }
              return s;
         }
         return new R().setCode(400);
     }




}

